// 获取数组 array 中属性名为 key 的值合成的数组
/**
  array=[{id:1,name:'a'},{id:2,name:b}]; key='id'
  结果：[1,2]
 */
export function getKeyArray(key, array) {
  let arr = []
  array.forEach(el => {
    arr.push(el[key])
  })
  return arr
}

// 根据某一属性(keyArr)合集数组，返回对应的对象数组
/**
  arr = [{id:1,name:a},{id:2,name:b},{id:3,name:c}]
  keyArr = [1,2]
  key='i'
  返回：[{id:1,name:a},{id:2,name:b}]
 */
export function getObjArray(keyArr, array, key = 'id') {
  let arr = array.filter(item => keyArr.indexOf(item[key]) > -1)
  return arr
}


// 根据 keyName 求两个数组的交集，keyName为数组内对象的属性名称
export function intersectionFun(arr1, arr2, keyName) {
  const arr3 = arr1.map(item => item[keyName])
  const arr4 = arr2.filter(x => new Set(arr3).has(x[keyName]))
  return arr4
}

/**
 columns:[{text:'1',value:'a'}]
 根据value的值获取对应的text值
 */
export function getColumnsText(value, columns, props = { text: 'text', value: 'value' }) {
  let text = ""
  // 多选时为数组
  if (Array.isArray(value)) {
    let arr = columns.filter(item => value.some(ele => ele === item[props.value]));
    text = getKeyArray(props.text, arr).join(",")
  } else {
    // 单选
    columns.forEach(el => {
      if (el[props.value] == value) {
        text = el[props.text]
      }
    })
  }
  return text
}